package encome.work.config;

import encome.work.dao.ResponseResult;
import encome.work.utils.FileException;
import encome.work.utils.ServiceException;
import encome.work.utils.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {RestController.class})
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public ResponseResult<String> exceptionHandler(ServiceException ex){
        log.error("业务逻辑异常 ==> " + ex.getMessage());
        return new ResponseResult<>(StatusCode.ERROR, ex.getMessage());
    }

    @ExceptionHandler(FileException.class)
    public ResponseResult<String> exceptionHandler(FileException ex){
        log.error("文件操作异常 ==> " + ex.getMessage());
        return new ResponseResult<>(StatusCode.ERROR, ex.getMessage());
    }
}
